﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BanDTBLL;
using PagedList;
namespace _1061102_1061114_1061129_1061132_1061141.Controllers
{ 
    public class TrangChuController : Controller
    {
        private ban_dien_thoai_dbEntities db = new ban_dien_thoai_dbEntities();

        //
        // GET: /TrangChu/

        public ViewResult Index()
        {
            var san_pham = from s in db.san_pham select s;
            ViewBag.SanPhamKM = from s in db.san_pham where s.trangThai == 4 select s;
            ViewBag.SanPhamMoi = (from s in db.san_pham orderby s.ma descending select s).Take(10);
            return View(san_pham);
        }

        public ViewResult TimKiemCoBan(int? page, string currentFilter, string SearchTen)
        {
            if (Request.HttpMethod == "GET")
            {
                currentFilter = SearchTen;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = SearchTen;
            var ds = from dt in db.san_pham
                     select dt;
            if (!string.IsNullOrEmpty(SearchTen))
            {
                ds = ds.Where(dt => dt.ten.Contains(SearchTen));
            }
            ds = ds.OrderBy(dt => dt.ten);
            int pageSize = 3;
            int pageNumber = (page ?? 1);
            return View(ds.ToPagedList(pageNumber, pageSize));
        }

        //
        // GET: /TrangChu/Details/5

        public ViewResult Details(int id)
        {
            san_pham san_pham = db.san_pham.Single(s => s.ma == id);
            return View(san_pham);
        }

        //
        // GET: /TrangChu/Create

        public ActionResult Create()
        {
            ViewBag.maLoaiSP = new SelectList(db.loai_san_pham, "ma", "tenLoai");
            ViewBag.trangThai = new SelectList(db.trang_thai_sp, "maTrangThai", "tenTrangThai");
            return View();
        } 

        //
        // POST: /TrangChu/Create

        [HttpPost]
        public ActionResult Create(san_pham san_pham)
        {
            if (ModelState.IsValid)
            {
                db.san_pham.AddObject(san_pham);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.maLoaiSP = new SelectList(db.loai_san_pham, "ma", "tenLoai", san_pham.maLoaiSP);
            ViewBag.trangThai = new SelectList(db.trang_thai_sp, "maTrangThai", "tenTrangThai", san_pham.trangThai);
            return View(san_pham);
        }
        
        //
        // GET: /TrangChu/Edit/5
 
        public ActionResult Edit(int id)
        {
            san_pham san_pham = db.san_pham.Single(s => s.ma == id);
            ViewBag.maLoaiSP = new SelectList(db.loai_san_pham, "ma", "tenLoai", san_pham.maLoaiSP);
            ViewBag.trangThai = new SelectList(db.trang_thai_sp, "maTrangThai", "tenTrangThai", san_pham.trangThai);
            return View(san_pham);
        }

        //
        // POST: /TrangChu/Edit/5

        [HttpPost]
        public ActionResult Edit(san_pham san_pham)
        {
            if (ModelState.IsValid)
            {
                db.san_pham.Attach(san_pham);
                db.ObjectStateManager.ChangeObjectState(san_pham, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.maLoaiSP = new SelectList(db.loai_san_pham, "ma", "tenLoai", san_pham.maLoaiSP);
            ViewBag.trangThai = new SelectList(db.trang_thai_sp, "maTrangThai", "tenTrangThai", san_pham.trangThai);
            return View(san_pham);
        }

        //
        // GET: /TrangChu/Delete/5
 
        public ActionResult Delete(int id)
        {
            san_pham san_pham = db.san_pham.Single(s => s.ma == id);
            return View(san_pham);
        }

        //
        // POST: /TrangChu/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            san_pham san_pham = db.san_pham.Single(s => s.ma == id);
            db.san_pham.DeleteObject(san_pham);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}